/**
 * Created by wlh on 16/3/21.
 */
'use strict';

var React = require("react");
var ReactDOM = require("react-dom");

var Timer = React.createClass({
    displayName: "Timer",
    getInitialState: function() {
        var start = this.props.start;
        var end = this.props.end;

        return {
            time: (start - end) > 0? (start- end): 0
        }
    },
    render: function() {
        return <span>{this.state.time}</span>
    },
    componentDidMount: function() {
        var self = this;
        var time = this.state.time;
        if (time >0 ) {
            var _time = setInterval(function() {
                time = time - 1;
                self.setState({time: time});
                if (time <= 0) {
                    clearInterval(_time);
                }
            }, self.props.inteval);
        }
    }
});

ReactDOM.render(<Timer start="100" end="10" inteval="1000"/>, document.getElementById("timer1"));
ReactDOM.render(<Timer start="200" end="100" inteval="100"/>, document.getElementById("timer2"));